/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pr3_ia1;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Darick
 */
public class hsocket extends Thread {
    public ServerSocket skServer;
    public Socket skClient;
    public int PUERTO;
    public DataInputStream entrada;
    public gui_pr3_ia1 gui_ext;

    
    public hsocket(gui_pr3_ia1 gui){
        gui_ext=gui;
    }
    
    @Override
    public void run(){
        try {
            skServer = new ServerSocket(PUERTO);
            System.out.println("Escucho el puerto " + PUERTO );
            while(true){
                try {
                    String leido_piezas;
                    String leido_tablero;
                    skClient = skServer.accept();
                    System.out.println("Peticion de Cliente Aceptada");
                    entrada = new DataInputStream(skClient.getInputStream());
                    leido_piezas = entrada.readUTF();
                    System.out.println(leido_piezas);
                    entrada = new DataInputStream(skClient.getInputStream());
                    leido_tablero = entrada.readUTF();
                    String bitac = entrada.readUTF();
                    int ntur = entrada.readInt();
                    System.out.println(leido_tablero);
                    System.out.println(bitac);
                    gui_ext.actualizar_t(leido_piezas, leido_tablero, bitac, ntur);
                    skClient.close();
                    System.out.println("Socket Cerrado");
                } catch (IOException ex) {
                    Logger.getLogger(hsocket.class.getName()).log(Level.SEVERE, null, ex);
                }                    
            }  
        } catch (IOException ex) {
            //Logger.getLogger(hsocket.class.getName()).log(Level.SEVERE, null, ex);
            //solucion no sirve, solo da advertencia pero no solucionado
//            gui_ext.btn2_juego.setSelected(false);
//            gui_ext.iniciar_detener();
            JOptionPane.showMessageDialog(gui_ext, "Jugador 1, ya ha existe, inicie como Jugador 2");
        }              
    }
}
